package com.ganbo.stock.service.impl;


import com.ganbo.stock.enums.ServiceType;
import com.ganbo.stock.utils.FastJsonUtil;
import com.ganbo.stock.utils.ThreadUtils;
import com.ganbo.stock.config.Ig507Config;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class BaseService {

    @Autowired
    RestTemplate restTemplate;
    @Autowired
    Ig507Config ig507Config;

    public <T> T sendReq(ServiceType serviceType, Map<String, String> params, Class<T> respClass) {
        T t = null;
        String returnStr = "";
        for (int i = 0; i < 3; i++) {
            try {
                ResponseEntity<String> response = restTemplate.getForEntity(serviceType.getUrl(), String.class, params);
                log.info("Ig507获取{}，返回信息:{}", serviceType.getDesc(), response.getBody());
                return FastJsonUtil.jsonStringToObject(response.getBody(), respClass);
            } catch (Exception e) {
                log.info("Ig507拉取股票信息失败,Ig507返回信息:{}，", returnStr, e);
            }
            ThreadUtils.sleepSeconds(2);
        }
        return t;
    }

    public <T> List<T> sendReqReturnList(ServiceType serviceType, Map<String, String> params, Class<T> respClass) {
        ResponseEntity<String> response = restTemplate.getForEntity(serviceType.getUrl(), String.class, params);
//        log.info("Ig507获取{}，返回信息:{}", serviceType.getDesc(), response.getBody());
        return FastJsonUtil.jsonArrayToObjectList(response.getBody(), respClass);
    }

}
